package com.enhe.gck.utils;

import com.enhe.gck.communal.PublicEntity;
import com.enhe.gck.communal.PublicEnum;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

@Slf4j
public class PDFUtil {
    public static ByteArrayOutputStream createPdf(byte[] inputStream, Map<String,Object> data){
        try {
            @Cleanup PdfReader reader = null;

            @Cleanup PdfStamper stamp = null;
            @Cleanup ByteArrayOutputStream baos = null;
            reader = new PdfReader(inputStream); // 读取pdf模板
            // 创建书写器
            baos = new ByteArrayOutputStream();
            stamp = new PdfStamper(reader, baos);
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            AcroFields form = stamp.getAcroFields();
            form.addSubstitutionFont(baseFont);
            Map<String, AcroFields.Item> fields = form.getFields();// 取出报表模板中的所有字段
            // 给每个字段填充数据
            for (String key :fields.keySet()){
                // 时间字段转换
                if (data.get(key) instanceof Date){
                    SimpleDateFormat simp = new SimpleDateFormat(PublicEnum.DateString.date11.getCode());
                    String time = simp.format(data.get(key));
                    form.setField(key, time);
                }
                form.setField(key, String.valueOf(data.get(key)));
            }
            // true表示生成的resultPDF不可再编辑
            stamp.setFormFlattening(true);
            return baos;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static ByteArrayOutputStream createPdfTm(byte[] inputStream, Map<String,Object> data, PublicEntity.Barcode barcode){
        try {
            @Cleanup PdfReader reader = null;
            @Cleanup PdfStamper stamp = null;
            @Cleanup ByteArrayOutputStream baos = null;
            reader = new PdfReader(inputStream); // 读取pdf模板
            // 创建书写器
            baos = new ByteArrayOutputStream();
            stamp = new PdfStamper(reader, baos);
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            AcroFields form = stamp.getAcroFields();
            form.addSubstitutionFont(baseFont);
            Map<String, AcroFields.Item> fields = form.getFields();// 取出报表模板中的所有字段
            // 给每个字段填充数据
            for (String key :fields.keySet()){
                // 时间字段转换
                if (data.get(key) instanceof Date){
                    SimpleDateFormat simp = new SimpleDateFormat(PublicEnum.DateString.date11.getCode());
                    String time = simp.format(data.get(key));
                    form.setField(key, time);
                }
                form.setField(key, String.valueOf(data.get(key)));
            }
            float x=barcode.getX();
            float y=barcode.getY();
            if(barcode.isFlagReverseY()){
                Rectangle pageSize = reader.getPageSize(1);
                float Height=pageSize.getHeight();
                y=Height-y;
            }
            Barcode128 barcode128 = new Barcode128();
            //条形码字号
            barcode128.setCode(barcode.getCode());
            barcode128.setBarHeight(barcode.getBarHeight());
            barcode128.setX(barcode.getScale());
            barcode128.setSize(barcode.getSize());
            barcode128.setBaseline(barcode.getBaseline());
            barcode128.setAltText(barcode.getCode());
            barcode128.setStartStopText(false);
            barcode128.setExtended(false);
            //绘制条形码在第一页
            PdfContentByte cb = stamp.getOverContent(1);
            //生成条形码图片
            com.itextpdf.text.Image image128 = barcode128.createImageWithBarcode(cb, null, null);
            //条形码位置
            image128.setAbsolutePosition(x,y);
            //加入条形码
            cb.addImage(image128);

            // true表示生成的resultPDF不可再编辑
            stamp.setFormFlattening(true);
            stamp.close();
            return baos;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }


    public static ByteArrayOutputStream DeliveryCode(byte[] inputStream, Map<String,Object> data){
        try {
            @Cleanup PdfReader reader = null;
            @Cleanup PdfStamper stamp = null;
            @Cleanup ByteArrayOutputStream baos = null;
            reader = new PdfReader(inputStream); // 读取pdf模板
            // 创建书写器
            baos = new ByteArrayOutputStream();
            stamp = new PdfStamper(reader, baos);
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            AcroFields form = stamp.getAcroFields();
            form.addSubstitutionFont(baseFont);
            Map<String, AcroFields.Item> fields = form.getFields();// 取出报表模板中的所有字段
            // 给每个字段填充数据
            for (String key :fields.keySet()){
                // 时间字段转换
                if (data.get(key) instanceof Date){
                    SimpleDateFormat simp = new SimpleDateFormat(PublicEnum.DateString.date11.getCode());
                    String time = simp.format(data.get(key));
                    form.setField(key, time);
                }
                form.setField(key, String.valueOf(data.get(key)));
            }
            //绘制条形码在第一页
            PdfContentByte cb = stamp.getOverContent(1);

            //生成条形码图片
            com.itextpdf.text.Image image128 = QRCodeGenerator.getEwCode(data.get("data42").toString(),130,130);
            if(image128!=null){
                image128.setAbsolutePosition(105,90);
            }
            cb.addImage(image128);

            if(image128!=null){
                image128.setAbsolutePosition(105,470);
            }
            cb.addImage(image128);

            // true表示生成的resultPDF不可再编辑
            stamp.setFormFlattening(true);
            stamp.close();
            return baos;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static ByteArrayOutputStream DeliveryCodeAnDa(byte[] inputStream, Map<String,Object> data){
        try {
            @Cleanup PdfReader reader = null;
            @Cleanup PdfStamper stamp = null;
            @Cleanup ByteArrayOutputStream baos = null;
            reader = new PdfReader(inputStream); // 读取pdf模板
            // 创建书写器
            baos = new ByteArrayOutputStream();
            stamp = new PdfStamper(reader, baos);
            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            AcroFields form = stamp.getAcroFields();
            form.addSubstitutionFont(baseFont);
            Map<String, AcroFields.Item> fields = form.getFields();// 取出报表模板中的所有字段
            // 给每个字段填充数据
            for (String key :fields.keySet()){
                // 时间字段转换
                if (data.get(key) instanceof Date){
                    SimpleDateFormat simp = new SimpleDateFormat(PublicEnum.DateString.date11.getCode());
                    String time = simp.format(data.get(key));
                    form.setField(key, time);
                }
                form.setField(key, String.valueOf(data.get(key)));
            }
            //绘制条形码在第一页
            PdfContentByte cb = stamp.getOverContent(1);

            //生成条形码图片
            com.itextpdf.text.Image image128 = QRCodeGenerator.getEwCode(data.get("data42").toString(),130,130);
            if(image128!=null){
                image128.setAbsolutePosition(105,440);
            }
            cb.addImage(image128);

            // true表示生成的resultPDF不可再编辑
            stamp.setFormFlattening(true);
            stamp.close();
            return baos;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    public static byte[] getBytes(byte[] inputStream, Map<String,Object> data){
        return Objects.requireNonNull(createPdf(inputStream, data)).toByteArray();
    }
    public static byte[] getBytes(byte[] inputStream, Map<String,Object> data,PublicEntity.Barcode barcode){
        return Objects.requireNonNull(createPdfTm(inputStream, data,barcode)).toByteArray();
    }

    public static byte[] getBytesDeliveryCode(byte[] inputStream, Map<String,Object> data){
        return Objects.requireNonNull(DeliveryCode(inputStream, data)).toByteArray();
    }
    public static byte[] getBytesDeliveryCodeAnDa(byte[] inputStream, Map<String,Object> data){
        return Objects.requireNonNull(DeliveryCodeAnDa(inputStream, data)).toByteArray();
    }
}
